package com.springboot.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.entity.Employee;
import com.springboot.mapper.EmployeeMapper;
import com.springboot.service.DeptService;
import com.springboot.service.EmployeeService;
import com.springboot.vo.EmployeeVo;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    private final DeptService deptService;
    public EmployeeServiceImpl(DeptService deptService) {
        this.deptService = deptService;
    }

    @Override
    public IPage<Employee> conditionFindByPage(Integer page, Integer pageSize, EmployeeVo employeeVo) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();

        if (employeeVo.getName() != null) {
            queryWrapper.like("name", employeeVo.getName());
        }
        if (employeeVo.getAddr() != null) {
            queryWrapper.like("addr", employeeVo.getAddr());
        }
        if (employeeVo.getDeptId() != null) {
            queryWrapper.eq("dept_id", employeeVo.getDeptId());
        }
        queryWrapper.eq("is_deleted", 0);
        Page<Employee> employeePage = page(new Page<>(page, pageSize), queryWrapper);
        employeePage.getRecords().forEach(employee -> {
            employee.setDeptName(deptService.getById(employee.getDeptId()).getDeptName());
        });
        return employeePage;
    }
}
